import "dart:ffi";
import 'dart:io';

import 'androidbindings.dart';

class AvPlayer {
  static DynamicLibrary? _library;
  static AVPlayerBindings? _bindings;

  static DynamicLibrary _loadLibrary() {
    if (_library != null) {
      return _library!;
    }

    if (Platform.isAndroid) {
      _library = DynamicLibrary.open("libAVPlayer.so");
    } else if (Platform.isIOS) {
      _library = DynamicLibrary.open("libiOSPlayerSDK.a");
    } else {
      throw UnsupportedError("This platform is not supported.");
    }

    return _library!;
  }

  static void initialize() {
    final library = _loadLibrary();
    _bindings = AVPlayerBindings(library);
  }

  static AVPlayerBindings get bindings {
    if (_bindings == null) {
      throw StateError(
        "AVPlayerBindings is not initialized. Call initialize() first.",
      );
    }
    return _bindings!;
  }
}
